package diploma.kisel.modelas.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.border.EmptyBorder;

import diploma.kisel.modelas.domain.Concept;
import diploma.kisel.modelas.gui.common.CurrentVar;
import diploma.kisel.modelas.service.ConceptServiceImpl;
import diploma.kisel.modelas.service.api.ConceptService;

/**
 * This window is designed for choosing current concept.
 * Users can create many concepts and they can change them
 * during the work.
 * 
 * @author Nikolay Kisel
 *
 */
public class CurrentConceptWindow extends JFrame {

	private JPanel contentPane;
	
	private static CurrentConceptWindow frame;
	private JComboBox comboBox;
//	private static Concept currConcept;
	
	private ConceptService conceptService = new ConceptServiceImpl();

	/**
	 * Create the frame.
	 */
	public CurrentConceptWindow() {
		setTitle("Текущая концепция");
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 349, 162);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);
		
		JLabel label = new JLabel("Выберите концепцию:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, label, 10, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, label, 10, SpringLayout.WEST, contentPane);
		contentPane.add(label);
		
		comboBox = createComboBox();
		sl_contentPane.putConstraint(SpringLayout.NORTH, comboBox, -3, SpringLayout.NORTH, label);
		sl_contentPane.putConstraint(SpringLayout.EAST, comboBox, -64, SpringLayout.EAST, contentPane);
		contentPane.add(comboBox);
		
		JButton buttonCancel = new JButton("Отмена");
		buttonCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setVisible(false);
			}
		});
		sl_contentPane.putConstraint(SpringLayout.SOUTH, buttonCancel, 0, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, buttonCancel, 0, SpringLayout.EAST, contentPane);
		contentPane.add(buttonCancel);
		
		JButton buttonOk = new JButton("Ок");
		buttonOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CurrentVar.setCurrentConcept((Concept) comboBox.getSelectedItem());
				frame.setVisible(false);
			}
		});
		sl_contentPane.putConstraint(SpringLayout.NORTH, buttonOk, 0, SpringLayout.NORTH, buttonCancel);
		sl_contentPane.putConstraint(SpringLayout.EAST, buttonOk, -6, SpringLayout.WEST, buttonCancel);
		contentPane.add(buttonOk);
	}
	
	public JComboBox createComboBox() {
		List<Concept> concepts =  conceptService.findByProject(CurrentVar.getCurrentProject());
		Vector<Object> items = new Vector<>();
		for (Concept concept : concepts) {
			items.add(concept);
		}
		comboBox = new JComboBox(items);
//		if (currConcept!=null)
//			comboBox.setSelectedItem(currConcept);
		return comboBox;
	}

	public CurrentConceptWindow getFrame() {
		return frame;
	}

	public void setFrame(CurrentConceptWindow frame) {
		this.frame = frame;
	}

	public void init() {
		Concept currConcept = CurrentVar.getCurrentConcept();
		if (currConcept!=null) 
			comboBox.setSelectedItem(currConcept);
	}
}
